<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>用户商品价格操作</p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body">
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>购买价格：</span>
          <div>
            <el-input type="number" placeholder="请输入购买价格" :class="{'error_border':tip_num===1}" v-model.trim="price"
                      @input="setTip('','')" min="1"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>最低购买量：</span>
          <div>
            <el-input type="number" placeholder="请输入最低购买量" :class="{'error_border':tip_num===2}"
                      v-model.trim="minNumber"
                      @input="setTip('','')" min="0"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>起送量：</span>
          <div>
            <el-input type="number" placeholder="请输入起送量" :class="{'error_border':tip_num===3}"
                      v-model.trim="minSendNumber"
                      @input="setTip('','')" min="0">
            </el-input>
          </div>
        </div>
        <p class="error_line">{{ infoTip }}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="judgeInfo">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import {getDetail, checkData} from "@/api/data/user/product";
import {judgeData} from "@/api/config/userProduct";

export default {
  props: ['productId'],
  data() {
    return {
      proId: '',
      price: 1,
      minNumber: 1,
      minSendNumber:1,
      userId: '',
      userOption: [],
      infoTip: '',
      tip_num: '',
      loading: false
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getDetail() {
      let that = this;
      this.loading = true;
      getDetail(that.productId).then(res => {
        if (res) {
          that.setData(res);
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setData(obj) {
      this.price = obj.price ? obj.price : 0;
      this.minNumber = obj.minNumber ? obj.minNumber : 0;
      this.minSendNumber = obj.minSendNumber ? obj.minSendNumber : 0;
      this.proId = obj.productId ? obj.productId : '';
      this.userId = obj.userId ? obj.userId : '';
    },

    judgeInfo(){
      let that = this;
      if (that.price === '') {
        that.setTip(1, '请输入购买价格！');
      } else if (that.price < 0) {
        that.setTip(1, '购买价格需大于0！');
      }
      else {
        let data = {
          productId: that.proId,
          price: that.price
        };
        judgeData(data).then((res) => {
          if (!res) {
            that.$confirm('修改价格需要审核，确定修改?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              that.confirmEdit()
            }).catch(() => { });
          }else{
            that.confirmEdit()
          }
        }).catch((e) => {})
      }
    },

    // 确认编辑
    confirmEdit() {
      if (this.minNumber === '') {
        this.setTip(2, '请输入最低购买量！');
      } else if (this.minNumber % 1 !== 0) {
        this.setTip(2, '最低购买量需为整数！');
      }
      else if (this.minSendNumber === '') {
        this.setTip(3, '请输入起送量！');
      } else if (this.minSendNumber % 1 !== 0 || this.minSendNumber <= 0) {
        this.setTip(3, '起送量需大于0且为整数！');
      }
      else {
        this.sendInfo();
      }
    },

    sendInfo() {
      let that = this;
      that.loading = true;
      let data = {
        productId: that.proId,
        userId: that.userId,
        price: that.price,
        minNumber: parseFloat(that.minNumber),
        minSendNumber: parseFloat(that.minSendNumber),
        id: that.productId
      }
      checkData(JSON.stringify(data)).then(response => {
        that.closePopup(true);
        that.$message.success({
          message:'操作成功！',
          duration:1500
        });
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setTip(num, text) {
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>
.half_box div {
  width: 124px !important;
}

.half_box .second_lab {
  width: 75px;
}
</style>
